import pymysql
#创建一个类，包含各种数据库函数的调用
class Result:
    #初始化函数，包含数据库url，密码，数据库名
    def __init__(self,**data):
        self.url = data['url']
        self.user = data['user']
        self.password =  data['password']
        self.db = data['db']
        

    def start(self):
        self.conn = pymysql.connect(host=self.url,user=self.user,password=self.password,db=self.db)
        self.cursor = self.conn.cursor()

    def rollback(self):
        self.conn.rollback()

    #查询函数，返回一个字典
    def query(self):
        self.cursor.execute("select * from face_codeing")
        # #获取查询结果
        result = self.cursor.fetchall()
        # #关闭游标
        return result   
    
    def query_by_class_name(self,class_):
        self.cursor.execute("select * from face_codeing where class_info = %s",(class_))
        # #获取查询结果
        result = self.cursor.fetchall()
        # #关闭游标
        return result   
    
    def insert(self,**data):
        sql = "insert into face_codeing(label,feature,class_info,chinese) values(%s,%s,%s,%s)"
        self.cursor.execute(sql,(data['label'],data['feature'],data['class_info'],data['cn_name']))
        self.conn.commit()

    
    def close(self):
        self.cursor.close()
        self.conn.close()

    def settings(self,**data):
        self.url = data['url']
        self.user = data['user']
        self.password = data['password']
        self.db = data['db']


 